﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RoboRun
{
    class MoveStraightSouth
    {
        public static async void moveRobotStraightSouth(int factor)
        {
            if (GameplayScreen.GridType.Equals("3x3"))
            {
                for (int i = 0; i < factor; i++)
                {
                    if (PrepareToPlay.userHasHitObstacleOrWall == true) //ensures go straight with parameters of more than 1 will not execute if user has hit an obstacle.
                    {
                        break;
                    }
                    int j = 0;
                    GameplayScreen.animationmarker = true;
                    RobotAnimation.straightAnimationSouth3x3();
                    GameplayScreen.animationmarker = true;
                    while (j < (350 * GameplayScreen.height / 1080) - (GameplayScreen.offset / 2))
                    {
                        GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                        GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                        if (checkIfObstacleAhead() == true)
                        {
                            PrepareToPlay.userHasHitObstacleOrWall = true;
                            moveUpToObstacle();
                            break;
                        }
                        if (checkIfGoalIsAhead() == true)
                        {
                            PrepareToPlay.userHasHitObstacleOrWall = true;
                            moveUpToGoal();
                            break;
                        }
                        if (checkIfGoingOffGrid() == true)
                        {
                            PrepareToPlay.userHasHitObstacleOrWall = true;
                            moveToEdgeOfGrid();
                            break;
                        }
                        await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                        j += 1;
                    }
                    GameplayScreen.animationmarker = false;
                    CalculateNewPosition.calculateNewPosition();
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1000));
                }
            }
            else if (GameplayScreen.GridType.Equals("4x4"))
            {
                for (int i = 0; i < factor; i++)
                {
                    if (PrepareToPlay.userHasHitObstacleOrWall == true) //ensures go straight with parameters of more than 1 will not execute if user has hit an obstacle.
                    {
                        break;
                    }
                    int j = 0;
                    GameplayScreen.animationmarker = true;
                    RobotAnimation.straightAnimationSouth4x4();
                    GameplayScreen.animationmarker = true;
                    while (j < (269 * GameplayScreen.height / 1080) - (GameplayScreen.offset / 2))
                    {
                        GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                        GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                        if (checkIfObstacleAhead() == true)
                        {
                            PrepareToPlay.userHasHitObstacleOrWall = true;
                            moveUpToObstacle();
                            break;
                        }
                        if (checkIfGoalIsAhead() == true)
                        {
                            PrepareToPlay.userHasHitObstacleOrWall = true;
                            moveUpToGoal();
                            break;
                        }
                        if (checkIfGoingOffGrid() == true)
                        {
                            PrepareToPlay.userHasHitObstacleOrWall = true;
                            moveToEdgeOfGrid();
                            break;
                        }
                        await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                        j += 1;
                    }
                    GameplayScreen.animationmarker = false;
                    CalculateNewPosition.calculateNewPosition();
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1000));
                }
            }
        }

        private static bool checkIfObstacleAhead()
        {
            char[] positionArray = GameplayScreen.RobotCurrentPosition.ToCharArray();
            char x = positionArray[0];
            char y = positionArray[2];
            int xToInt = Int32.Parse(x.ToString());
            int newX = xToInt + 1;
            String posToCheck = (newX + "," + y);
            for (int i = 0; i < GameplayScreen.ObstaclePositions.Length; i++)
            {
                if (GameplayScreen.ObstaclePositions[i].Equals(posToCheck))
                {
                    return true;
                }
            }

            return false;
        }


        private async static void moveUpToObstacle()
        {
            if (GameplayScreen.GridType.Equals("3x3"))
            {
                int j = 0;
                GameplayScreen.animationmarker = true;
                RobotAnimation.straightAnimationSouth3x3();
                while (j < 200 * GameplayScreen.height / 1080 - GameplayScreen.offset)
                {
                    GameplayScreen.animationmarker = true;
                    GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                    GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                    j += 1;
                }
                GameplayScreen.animationmarker = false;
                GameplayScreen.commandExecuted = "Game Over Obstacle";
                GameOver.callGameOverPrompt();
            }
            else if (GameplayScreen.GridType.Equals("4x4"))
            {
                int j = 0;
                GameplayScreen.animationmarker = true;
                RobotAnimation.straightAnimationSouth4x4();
                while (j < 155 * GameplayScreen.height / 1080 - GameplayScreen.offset)
                {
                    GameplayScreen.animationmarker = true;
                    GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                    GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                    j += 1;
                }
                GameplayScreen.animationmarker = false;
                GameplayScreen.commandExecuted = "Game Over Obstacle";
                GameOver.callGameOverPrompt();

            }
        }

        private static bool checkIfGoingOffGrid()
        {

            if (GameplayScreen.GridType.Equals("3x3"))
            {
                if (GameplayScreen.RobotCurrentPosition.Equals("3,1") || GameplayScreen.RobotCurrentPosition.Equals("3,2") || GameplayScreen.RobotCurrentPosition.Equals("3,3"))
                {
                    return true;
                }

            }
            else if (GameplayScreen.GridType.Equals("4x4"))
            {
                if (GameplayScreen.RobotCurrentPosition.Equals("4,1") || GameplayScreen.RobotCurrentPosition.Equals("4,2") || GameplayScreen.RobotCurrentPosition.Equals("4,3") || GameplayScreen.RobotCurrentPosition.Equals("4,4"))
                {
                    return true;
                }
            }
            return false;
        }

        private async static void moveToEdgeOfGrid() //move sprite towards wall then call GAME OVER
        {
            if (GameplayScreen.GridType.Equals("3x3"))
            {
                int j = 0;
                GameplayScreen.animationmarker = true;
                RobotAnimation.straightAnimationSouth3x3();
                while (j < 120 * GameplayScreen.height / 1080 - GameplayScreen.offset)
                {
                    GameplayScreen.animationmarker = true;
                    GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                    GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                    j += 1;
                }
                GameplayScreen.animationmarker = false;
                GameplayScreen.commandExecuted = "Game Over Wall";
                GameOver.callGameOverPrompt(); 
            }
            else if (GameplayScreen.GridType.Equals("4x4"))
            {
                int j = 0;
                GameplayScreen.animationmarker = true;
                RobotAnimation.straightAnimationSouth4x4();
                while (j < 140 * GameplayScreen.height / 1080 - GameplayScreen.offset)
                {
                    GameplayScreen.animationmarker = true;
                    GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                    GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                    j += 1;
                }
                GameplayScreen.animationmarker = false;
                GameplayScreen.commandExecuted = "Game Over Wall";
                GameOver.callGameOverPrompt(); 
            }
        }

        private static bool checkIfGoalIsAhead()
        {
            if (GameplayScreen.GridType.Equals("3x3"))
            {
                switch (GameplayScreen.RobotCurrentPosition)
                {
                    case "2,2":
                        
                            if (GameplayScreen.GoalPosition.Equals("3,2"))
                            {
                                return true;
                            }
                        
                        break;
                    case "2,3":
                        
                            if (GameplayScreen.GoalPosition.Equals("3,3"))
                            {
                                return true;
                            }
                        
                        break;
                    
                    case "2,1":
                        
                            if (GameplayScreen.GoalPosition.Equals("3,1"))
                            {
                                return true;
                            }
                        
                        break;
                    case "1,1":
                       
                            if (GameplayScreen.GoalPosition.Equals("2,1"))
                            {
                                return true;
                            }
                        
                        break;
                    case "1,2":
                        
                            if (GameplayScreen.GoalPosition.Equals("2,2"))
                            {
                                return true;
                            }
                        
                        break;
                    case "1,3":
                        
                            if (GameplayScreen.GoalPosition.Equals("2,3"))
                            {
                                return true;
                            }
                        
                        break;
                }

            }

            else if (GameplayScreen.GridType.Equals("4x4"))
            {
                switch (GameplayScreen.RobotCurrentPosition)
                {
                    case "2,2":

                        if (GameplayScreen.GoalPosition.Equals("3,2"))
                        {
                            return true;
                        }

                        break;
                    case "2,3":

                        if (GameplayScreen.GoalPosition.Equals("3,3"))
                        {
                            return true;
                        }

                        break;
                    case "2,4":
                        if (GameplayScreen.GoalPosition.Equals("3,4"))
                        {
                            return true;
                        }

                        break;
                    case "2,1":

                        if (GameplayScreen.GoalPosition.Equals("3,1"))
                        {
                            return true;
                        }

                        break;
                    case "1,1":

                        if (GameplayScreen.GoalPosition.Equals("2,1"))
                        {
                            return true;
                        }

                        break;
                    case "1,2":

                        if (GameplayScreen.GoalPosition.Equals("2,2"))
                        {
                            return true;
                        }

                        break;
                    case "1,3":

                        if (GameplayScreen.GoalPosition.Equals("2,3"))
                        {
                            return true;
                        }

                        break;
                    case "1,4":

                        if (GameplayScreen.GoalPosition.Equals("2,4"))
                        {
                            return true;
                        }

                        break;
                    case "3,1":

                        if (GameplayScreen.GoalPosition.Equals("4,1"))
                        {
                            return true;
                        }

                        break;
                    case "3,2":

                        if (GameplayScreen.GoalPosition.Equals("4,2"))
                        {
                            return true;
                        }

                        break;
                    case "3,3":

                        if (GameplayScreen.GoalPosition.Equals("4,3"))
                        {
                            return true;
                        }

                        break;
                    case "3,4":

                        if (GameplayScreen.GoalPosition.Equals("4,4"))
                        {
                            return true;
                        }

                        break;
                }

            }
            return false;
        }

        private async static void moveUpToGoal()
        {
            if (GameplayScreen.GridType.Equals("3x3"))
            {
                int j = 0;
                GameplayScreen.animationmarker = true;
                RobotAnimation.straightAnimationSouth3x3();
                while (j < 200 * GameplayScreen.height / 1080 - GameplayScreen.offset)
                {
                    GameplayScreen.animationmarker = true;
                    GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                    GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                    j += 1;
                }
                GameplayScreen.animationmarker = false;
                GameplayScreen.commandExecuted = "Player Won";
                PlayerWon.playerWonPrompt(); 
            }
            else if (GameplayScreen.GridType.Equals("4x4"))
            {
                int j = 0;
                GameplayScreen.animationmarker = true;
                RobotAnimation.straightAnimationSouth4x4();
                while (j < 155 * GameplayScreen.height / 1080 - GameplayScreen.offset)
                {
                    GameplayScreen.animationmarker = true;
                    GameplayScreen.robotSpriteMargin.Top = GameplayScreen.robotSpriteMargin.Top + 1;
                    GameplayScreen.RobotSpriteRef.Margin = GameplayScreen.robotSpriteMargin;
                    await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
                    j += 1;
                }
                GameplayScreen.animationmarker = false;
                GameplayScreen.commandExecuted = "Player Won";
                PlayerWon.playerWonPrompt();
            }
        }
    }
}
